import { createRouter, createWebHistory } from 'vue-router'
import storage from '@/utils/storage'
import login from '../views/login.vue'
import updatePwd from '../views/updatePwd.vue'
import Main from '../views/Main.vue'
import HomeView from '../views/HomeView.vue'
import EnergyUseView from '../views/EnergyUseView.vue'
import RealTimeMonitorView from '../views/RealTimeMonitorView.vue'
import MaximumView from '../views/MaximumView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "otherRouter",
      redirect: "/home",
      component: Main,
      children: [
        {
          path: '/home',
          name: 'home',
          component: HomeView,
        },
        {
          path: '/energyuse',
          name: 'energyuse',
          meta: {
            menu: true
          },
          component: EnergyUseView
        },
        {
          path: '/realtimemonitor',
          name: 'realtimemonitor',
          meta: {
            menu: true
          },
          component: RealTimeMonitorView
        },
        {
          path: '/maximum',
          name: 'maximum',
          meta: {
            menu: true
          },
          component: MaximumView
        }
      ]
    },

    {
      path: '/updatePwd',
      name: 'updatePwd',
      component: updatePwd,
    },{
      path: '/login',
      name: 'login',
      component: login,
    },
  ]
})

router.beforeEach((to, from, next) => {
  console.log(storage.get('token'))
  if ((storage.get('token') && storage.get('userInif')) || to.path == '/login') {
    next();
  } else {
    next('/login');
  }
})

export default router
